﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 战斗角色信息
/// </summary>
public class FightPersonageInfo : MonoBehaviour {

    /// <summary>
    /// 队伍序数（-1表示敌人，1表示友方）
    /// </summary>
    public int TeamIndex = -1;
    /// <summary>
    /// 队伍中的人员序数（0中间，1左边，2右边）
    /// </summary>
    public int PersonageIndex;

    private Text Name;
    private Text Hp;
    private Text Mp;
    private Text Vit;
    private Text Status;
    private Slider HpSlider;
    private Slider MpSlider;
    private Slider VitSlider;
    /// <summary>
    /// 对应人物数据
    /// </summary>
    private Personage ThePersonage;
    private int CurrentHp;
    private int CurrentMp;
    private int CurrentVit;

	// Use this for initialization
	void Start () {
        Text[] texts = GetComponentsInChildren<Text>();
        foreach (Text text in texts) {
            string name = text.name;
            if (name.Equals("Name")) {
                this.Name = text;
            } else if (name.Equals("HpText")) {
                this.Hp = text;
            } else if (name.Equals("MpText")) {
                this.Mp = text;
            } else if (name.Equals("VitText")) {
                this.Vit = text;
            } else if (name.Equals("Status")) {
                this.Status = text;
            }
        }

        Slider[] sliders = GetComponentsInChildren<Slider>();
        foreach (Slider slider in sliders) {
            string name = slider.name;
            if (name.Equals("HpSlider")) {
                this.HpSlider = slider;
            } else if (name.Equals("MpSlider")) {
                this.MpSlider = slider;
            } else if (name.Equals("VitSlider")) {
                this.VitSlider = slider;
            }
        }

        GlobalData globalData = GlobalData.Instance;
        if (this.TeamIndex == -1 && globalData.EnemyFightTeam.Count > this.PersonageIndex) { // 如果是敌人 && 敌人队伍人数大于人物序数
            this.ThePersonage = globalData.EnemyFightTeam[this.PersonageIndex];
            this.Name.text = ThePersonage.LastName + ThePersonage.FirstName;
        } else if (this.TeamIndex == 1 && globalData.OurFightTeam.Count > this.PersonageIndex) { // 如果是友方 && 友方队伍人数大于人物序数
            this.ThePersonage = globalData.OurFightTeam[this.PersonageIndex];
            this.Name.text = ThePersonage.LastName + ThePersonage.FirstName;
        } else { // 其他情况隐藏
            this.gameObject.SetActive(false);
        }

        if (this.ThePersonage != null) {
            this.HpSlider.maxValue = this.ThePersonage.MaxHp;
            this.MpSlider.maxValue = this.ThePersonage.MaxMp;
            this.VitSlider.maxValue = this.ThePersonage.MaxVit;
        }

    }
	
	// Update is called once per frame
	void Update () {
		if (this.ThePersonage != null) {
            this.ShowInfo();
        }
	}

    private void ShowInfo() {
        if (this.CurrentHp != this.ThePersonage.Hp) {
            this.Hp.text = this.ThePersonage.Hp + "/" + this.ThePersonage.MaxHp;
            this.HpSlider.value = this.ThePersonage.Hp;
            this.Status.text = "状态：" + (this.ThePersonage.GetHpStatus() == null ? "正常" : this.ThePersonage.GetHpStatus());
            this.CurrentHp = this.ThePersonage.Hp;
        }
        if (this.CurrentMp != this.ThePersonage.Mp) {
            this.Mp.text = this.ThePersonage.Mp + "/" + this.ThePersonage.MaxMp;
            this.MpSlider.value = this.ThePersonage.Mp;
            this.CurrentMp = this.ThePersonage.Mp;
        }
        if (this.CurrentVit != this.ThePersonage.Vit) {
            this.Vit.text = this.ThePersonage.Vit + "/" + this.ThePersonage.MaxVit;
            this.VitSlider.value = this.ThePersonage.Vit;
            this.CurrentVit = this.ThePersonage.Vit;
        }
    }
}
